package code.main.modle.dnAlg
{
	import code.main.modle.table.CardVo;
	
	import com.yy.tools.arry.ArrayTools;

	public class DnAlg
	{
		public static var COW_NO:int = 10;//牛
		public static var COW_1:int = 21;//牛1
		public static var COW_2:int = 22;//牛2
		public static var COW_3:int = 23;//牛3
		public static var COW_4:int = 24;//牛4
		public static var COW_5:int = 25;//牛5
		public static var COW_6:int = 26;//牛6
		public static var COW_7:int = 27;//牛7
		public static var COW_8:int = 28;//牛8
		public static var COW_9:int = 29;//牛9
		public static var COW_NIOU_NIOU:int = 30;//牛牛
		public static var COW_YING_NIOU:int = 31;//银牛
		public static var COW_SI_ZHA:int = 31;//四炸
		public static var COW_WU_HUA:int = 31;//五花
		public static var COW_WU_XIAO:int = 31;//五小
		
		private static var _instance:DnAlg;
		public function DnAlg(xx:XX)
		{
			
		}
		
		public static function getInstance():DnAlg {
			if(!_instance) {
				_instance = new DnAlg(new XX());
			}
			return _instance;
		}
		
		/**
		 *计算牛值 
		 * @param arr
		 * @return 
		 * 
		 */
		public function getCowValue(arr:Array):int {
			if(isSiZha(arr)) {
				return COW_SI_ZHA;
			}
			if(isWuXiao(arr)) {
				return COW_WU_XIAO;
			}
			if(isWuHua(arr)) {
				return COW_WU_HUA;
			}
			if(isYinNiou(arr)) {
				return COW_YING_NIOU;
			}
			return countNiou(arr); 
		}
		
		/**
		 *计算常规牛值 
		 * @param arr
		 * @return 
		 * 
		 */
		private function countNiou(arr:Array):int {
			var arr1:Array = [];//选择的牌的数组
			var arr2:Array = [];//没有被选择的牌的数组
			var i:int;
			var len:int = arr.length;
			var vo:CardVo;
			for(i=0; i<len; i++) {
				vo = arr[i];
				if(vo.selectBoo) {
					arr1.push(vo.id>10?10:vo.id);
				}else {
					arr2.push(vo.id>10?10:vo.id); 
				}
			}
			var sum:int = 0;
			if(arr1.length == 3){
				sum = ArrayTools.sum(arr1);
				if(sum != 0 && sum%10==0) {
					sum = ArrayTools.sum(arr2);
					if(sum%10 == 0) {
						return COW_NIOU_NIOU;
					}else {
						return DnAlg["COW_" + sum%10];
					}
				}
				return COW_NO;
			}
			if(arr2.length == 3) {
				sum = ArrayTools.sum(arr2);
				if(sum != 0 && sum%10==0) {
					sum = ArrayTools.sum(arr1);
					if(sum%10 == 0) {
						return COW_NIOU_NIOU;
					}else {
						return DnAlg["COW_" + sum%10];
					}
				}
				return COW_NO;
			}
			return COW_NO;
		}
		
		/**
		 *是否是4炸 
		 * @return 
		 * 
		 */
		private function isSiZha(arr:Array):Boolean {
			var vo:CardVo;
			var i:int;
			var len:int = 5;
			var obj:Object = {};
			for(i=0; i<len; i++) {
				vo = arr[i];
				if(obj[vo.id]){
					obj[vo.id]++;
				}else {
					obj[vo.id] = 1;
				}
			}
			for(var j:* in obj) {
				if(obj[j] == 4) {
					return true;
				}
			}
			return false;
		}
		
		/**
		 *是否是五小 
		 * @param arr
		 * 
		 */
		private function isWuXiao(arr:Array):Boolean {
			var sum:int = 0;
			var vo:CardVo;
			var i:int;
			var len:int = arr.length;
			for(i=0; i<len; i++) {
				vo = arr[i];
				if(vo.id > 4) {
					return false;
				}
				sum += vo.id;
			}
			if(sum < 11) {
				return true;
			}else {
				return false;
			}
		}
		/**
		 * 是否是五花 (金牛)
		 * @param arr
		 * 
		 */
		private function isWuHua(arr:Array):Boolean {
			var i:int;
			var len:int = arr.length;
			var vo:CardVo;
			for(i=0; i<len; i++) {
				vo = arr[i];
				if(vo.id < 11) {
					return false;
				}
			}
			return true;
		}
		
		/**
		 *是否是银牛 
		 * @param arr
		 * @return 
		 * 
		 */
		private function isYinNiou(arr:Array):Boolean {
			var i:int;
			var len:int = arr.length;
			var vo:CardVo;
			for(i=0; i<len; i++) {
				vo = arr[i];
				if(vo.id < 10) {
					return false;
				}
			}
			return true;
		}
		
		/**
		 *通过牛值返回赔率 
		 * @param cowValue
		 * @return 
		 * 
		 */
		public function getOddsByCow(cowValue:int):int {
			var odds:int = 1;
			switch(cowValue) {
				case COW_WU_XIAO:
					odds = 7;
					break;
				case COW_WU_HUA:
					odds = 5;
					break;
				case COW_YING_NIOU:
					odds = 4;
					break;
				case COW_NIOU_NIOU:
					odds = 3;
					break;
				case COW_9:
				case COW_8:
				case COW_7:
					odds = 2;
					break;
				default:
					odds = 1;
			}
			return odds;
		}
	}
}

class XX{}